package cn.fw.community.adapter.facade.controller;


import cn.fw.community.application.service.test.TestAppService;
import cn.fw.community.infrastructure.test.dao.TestDao;
import cn.fw.community.infrastructure.test.entity.Test;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.gitee.chearnee.fw.datasecurity.core.annotation.control.DisableSD;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Test)表控制层
 *
 * @author makejava
 * @since 2023-03-01 15:31:16
 */
@RestController
@DisableSD
@RequiredArgsConstructor
@RequestMapping("test")
public class TestController {
    /**
     * 服务对象
     */
    @Resource
    private TestDao testDao;
    private final TestAppService testAppService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param test 查询实体
     * @return 所有数据
     */
    @GetMapping
    public IPage<Test> selectAll(Page<Test> page, Test test) {
        return this.testDao.page(page, new QueryWrapper<>(test));
    }

    @GetMapping("t")
    public Test getTest() {
        Test test = new Test();
//        test.setOperationType(OperationType.EXPORT);
        return test;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Test selectOne(@PathVariable Long id) {
        return this.testDao.getById(id);
    }

    /**
     * 新增数据
     *
     * @param test 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Long insert(@RequestBody Test test) {
        this.testDao.save(test);
        return test.getId();
    }

    /**
     * 修改数据
     *
     * @param test 实体对象
     * @return 修改结果
     */
    @PutMapping
    public boolean update(@RequestBody Test test) {
        return this.testDao.updateById(test);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public boolean delete() {
        testDao.remove(Wrappers.<Test>lambdaQuery().eq(Test::getCode, "2"));
        return false;
    }
}

